package com.hujiheng.paike.controller;

import com.hujiheng.paike.common.lang.Result;
import com.hujiheng.paike.pojo.Course;
import com.hujiheng.paike.pojo.Teacher;
import com.hujiheng.paike.service.CourseService;
import com.hujiheng.paike.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/list")
    public Result list() {
        List<Course> courseList = courseService.getCourseList();
        return Result.success(courseList);
    }

    @RequestMapping(value = "/getTeacher/list", method = RequestMethod.POST)
    public Result getTeacherList(@RequestBody Course course) {
        Course courseByName = courseService.getCourseByName(course.getCourseName());
        List<Teacher> teachersByCourseId = teacherService.getTeachersByCourseId(courseByName.getCourseId());
        if (teachersByCourseId.size() == 0) {
            return Result.fail("没有结果");
        }
        System.out.println(teachersByCourseId);
        return Result.success(teachersByCourseId);
    }

    @RequestMapping("/getTeacherList/{courseId}")
    public Result getTeacherList(@PathVariable("courseId") int courseId) {
        List<Teacher> teachersByCourseId = teacherService.getTeachersByCourseId(courseId);
        if (teachersByCourseId.size() == 0) {
            return Result.fail("没有结果");
        }
        System.out.println(teachersByCourseId);
        return Result.success(teachersByCourseId);
    }

    @RequestMapping("/add")
    public Result add(@Validated @RequestBody Course course) {
        int addCourse = courseService.addCourse(course);
        if (addCourse < 1) {
            return Result.fail("添加失败");
        }
        return Result.success("添加成功");
    }

    @RequestMapping("/del/{courseId}")
    public Result del(@PathVariable("courseId") int courseId) {
        //获取所有教师信息
        List<Teacher> teachersByCourseId = teacherService.getTeachersByCourseId(courseId);
        //获取所有课程信息
        List<Course> courseList = courseService.getCourseList();
        //给所有该课程的教师随机分配工作
        for (Teacher teacher : teachersByCourseId) {
            int nextId;
            while ((nextId = new Random().nextInt(courseList.size())) == courseId);
            teacher.setCourseId(courseList.get(nextId).getCourseId());
            teacherService.updateTeacher(teacher);
        }
        //删除课程
        int delCourse = courseService.delCourse(courseId);
        if (delCourse < 1) {
            for (Teacher teacher : teachersByCourseId) {
                teacher.setCourseId(courseId);
                teacherService.updateTeacher(teacher);
            }
            return Result.fail("删除失败");
        }

        return Result.success("可喜可贺");
    }
}
